#!/bin/sh

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null

[ ! -d $HOME/.config/gtk-2.0 ] && mkdir $HOME/.config/gtk-2.0
[ ! -d $HOME/.config/gtk ] && mkdir $HOME/.config/gtk
WIDGETS=$HOME/.config/gtk-2.0/gtkrc.widgets
WIDGETS1=$HOME/.config/gtk/gtkrc.widgets
CURRENTWIDGETS="`cat $WIDGETS | grep gtkrc | cut -f 5 -d '/'`"
FONTS=$HOME/.config/gtk-2.0/gtkrc.fonts
CURRENTFONT="`cat $FONTS | grep gtk-font-name | cut -f 2 -d '"'`"
ICONS=$HOME/.config/gtk-2.0/gtkrc.icons
CURRENTICONS="`cat $ICONS | grep gtk-icon-theme | cut -f 2 -d '"'`"

case $1 in
	-widgets) #set the widget theme
	[ -f $WIDGETS ] && cp -f $WIDGETS $WIDGETS.bak
	THEMELOC="`ls -d /usr/share/themes/* $HOME/.themes/*`"
	THEMES="`cat $HOME/.config/themetmp | cut -f 1 -d ',' | tr "\n" " "`"
	NEWTHEME=$2
	NEWTHEMERC="`cat $HOME/.config/themetmp | grep $NEWTHEME,/ \
	 | cut -f 2 -d ','`"
	echo '# machine written -- do not edit' > $WIDGETS
	echo "include \"$NEWTHEMERC\"" >> $WIDGETS
	if [ -f "/usr/share/themes/$NEWTHEME/gtk/gtkrc" ];then
	  echo '# machine written -- do not edit' > $WIDGETS1
	  echo "include \"/usr/share/themes/$NEWTHEME/gtk/gtkrc\"" >> $WIDGETS1
	fi
	gtkrefresh
	;;
	
	-icons) #set the icon theme
	[ -f $ICONS ] && cp -f $ICONS $ICONS.bak
	NEWICONS=$2
	echo '# machine written -- do not edit' > $ICONS
	echo "gtk-icon-theme-name = \"$NEWICONS\"" >> $ICONS
	[ -f $HOME/.config/icontmp ] && rm -f $HOME/.config/icontmp
	gtkrefresh
	;;
	
	-fonts) #set the font
	[ -f $FONTS ] && cp -f $FONTS $FONTS.bak
	OLDFONT="`cat $FONTS | grep gtk-font-name | cut -f 2 -d '='`"
	FONT="`Xdialog --stdout --title "Set gtk+ font" --icon \
	"/usr/share/icons/Tango/scalable/categories/preferences-system.svg" \
	--no-cancel --fontsel $OLDFONT "$LOC_CHOOSEFONT\n\
$LOC_CURRENTFONT $CURRENTFONT." 0 0`"
	[ ! $? -eq 0 ] && main_window
	echo '# machine written -- do not edit' > $FONTS
	echo 'style "user-font"' >> $FONTS
	echo '{' >> $FONTS
	echo "font_name = \"$FONT\"" >> $FONTS
	echo '}' >> $FONTS
	echo 'widget_class "*" style "user-font"' >> $FONTS
	echo "gtk-font-name=\"$FONT\"" >> $FONTS
	gtkrefresh
	;;
	-preview)
	export PREVIEW_DIALOG="
<window window-position=\"2\" title=\"$LOC_PREVIEW_TITLE\" icon_name=\"gtk-find\">
 <vbox>
  <menubar>
   <menu>
    <menuitem icon=\"gtk-new\">
     <label>$LOC_NEW</label>
     <action>echo new</action>
    </menuitem>
    <menuitem icon=\"gtk-copy\">
     <label>$LOC_COPY</label>
     <action>echo copy</action>
    </menuitem>
    <separator></separator>
    <menuitem stock=\"gtk-quit\">
     <action>EXIT:abort</action>
    </menuitem>
    <label>$LOC_FILE</label>
   </menu>
   <menu>
    <menuitem icon=\"gtk-help\">
     <label>$LOC_HELP</label>
     <action>echo help</action>
    </menuitem>
    <label>$LOC_HELP</label>
   </menu>
  </menubar>
  <notebook labels=\"$LOC_TABS\">
   <vbox>
    <hbox>
     <text>
      <label>$LOC_COMBOBOX</label>
     </text>
     <combobox>
      <item>${LOC_ITEM}1</item>
      <item>${LOC_ITEM}2</item>
      <item>${LOC_ITEM}3</item>
     </combobox>
    </hbox>
    <hbox>
     <vbox>
      <hbox>
       <checkbox>
        <label>Checkbox1</label>
        <variable>${LOC_CHECKBOX}1</variable>
        <action>if true enable:ENTRY</action>
        <action>if false disable:ENTRY</action>
        <action>if true enable:BUTTON2</action>
        <action>if false disable:BUTTON2</action>
       </checkbox>
       <checkbox>
        <label>${LOC_CHECKBOX}2</label>
        <action>if true disable:CHECKBOX1</action>
        <action>if false enable:CHECKBOX1</action>
        <action>if true disable:BUTTON1</action>
        <action>if false enable:BUTTON1</action>
       </checkbox>
      </hbox>
      <hbox>
       <checkbox draw_indicator=\"false\">
        <label>${LOC_BUTTON}1</label>
        <variable>BUTTON1</variable>
       </checkbox>
       <button sensitive=\"false\">
        <label>${LOC_BUTTON}2</label>
        <variable>BUTTON2</variable>
        <action>echo BUTTON2</action>
       </button>
      </hbox>
      <entry>
        <default>$LOC_DEFAULT_TEXT</default>
        <variable>ENTRY</variable>
        <visible>disabled</visible>
      </entry>
     </vbox>
     <frame $LOC_FRAME>
      <vbox>
       <radiobutton>
        <label>$LOC_RADIOBUTTON 1</label>
       </radiobutton>
       <radiobutton>
        <label>$LOC_RADIOBUTTON 2</label>
       </radiobutton>
      </vbox>
     </frame>
    </hbox>
    <progressbar>
     <label>$LOC_PROGRESSBAR</label>
     <input>echo 50</input>
    </progressbar>
    <edit>
     <height>70</height>
     <width>250</width>
     <default>$LOC_EDITOR</default>
    </edit>
   </vbox>
   <vbox>
    <tree>
     <height>200</height>
     <width>250</width>
     <label>${LOC_STOCK}| </label>
     <item stock=\"gtk-dialog-authentication\">gtk-dialog-authentication</item>
     <item stock=\"gtk-dialog-info\">gtk-dialog-info</item>
     <item stock=\"gtk-dialog-warning\">gtk-dialog-warning</item>
     <item stock=\"gtk-dialog-error\">gtk-dialog-error</item>
     <item stock=\"gtk-dialog-question\">gtk-dialog-question</item>
     <item stock=\"gtk-dnd\">gtk-dnd</item>
     <item stock=\"gtk-dnd-multiple\">gtk-dnd-multiple</item>
     <item stock=\"gtk-about\">gtk-about</item>
     <item stock=\"gtk-add\">gtk-add</item>
     <item stock=\"gtk-apply\">gtk-apply</item>
     <item stock=\"gtk-bold\">gtk-bold</item>
     <item stock=\"gtk-cancel\">gtk-cancel</item>
     <item stock=\"gtk-cdrom\">gtk-cdrom</item>
     <item stock=\"gtk-clear\">gtk-clear</item>
     <item stock=\"gtk-close\">gtk-close</item>
     <item stock=\"gtk-color-picker\">gtk-color-picker</item>
     <item stock=\"gtk-convert\">gtk-convert</item>
     <item stock=\"gtk-connect\">gtk-connect</item>
     <item stock=\"gtk-copy\">gtk-copy</item>
     <item stock=\"gtk-cut\">gtk-cut</item>
     <item stock=\"gtk-delete\">gtk-delete</item>
     <item stock=\"gtk-directory\">gtk-directory</item>
     <item stock=\"gtk-disconnect\">gtk-disconnect</item>
     <item stock=\"gtk-edit\">gtk-edit</item>
     <item stock=\"gtk-execute\">gtk-execute</item>
     <item stock=\"gtk-file\">gtk-file</item>
     <item stock=\"gtk-find\">gtk-find</item>
     <item stock=\"gtk-find-and-replace\">gtk-find-and-replace</item>
     <item stock=\"gtk-floppy\">gtk-floppy</item>
     <item stock=\"gtk-fullscreen\">gtk-fullscreen</item>
     <item stock=\"gtk-goto-bottom\">gtk-goto-bottom</item>
     <item stock=\"gtk-goto-first\">gtk-goto-first</item>
     <item stock=\"gtk-goto-last\">gtk-goto-last</item>
     <item stock=\"gtk-goto-top\">gtk-goto-top</item>
     <item stock=\"gtk-go-back\">gtk-go-back</item>
     <item stock=\"gtk-go-down\">gtk-go-down</item>
     <item stock=\"gtk-go-forward\">gtk-go-forward</item>
     <item stock=\"gtk-go-up\">gtk-go-up</item>
     <item stock=\"gtk-harddisk\">gtk-harddisk</item>
     <item stock=\"gtk-help\">gtk-help</item>
     <item stock=\"gtk-home\">gtk-home</item>
     <item stock=\"gtk-index\">gtk-index</item>
     <item stock=\"gtk-indent\">gtk-indent</item>
     <item stock=\"gtk-info\">gtk-info</item>
     <item stock=\"gtk-unindent\">gtk-unindent</item>
     <item stock=\"gtk-italic\">gtk-italic</item>
     <item stock=\"gtk-jump-to\">gtk-jump-to</item>
     <item stock=\"gtk-justify-center\">gtk-justify-center</item>
     <item stock=\"gtk-justify-fill\">gtk-justify-fill</item>
     <item stock=\"gtk-justify-left\">gtk-justify-left</item>
     <item stock=\"gtk-justify-right\">gtk-justify-right</item>
     <item stock=\"gtk-leave-fullscreen\">gtk-leave-fullscreen</item>
     <item stock=\"gtk-missing-image\">gtk-missing-image</item>
     <item stock=\"gtk-media-forward\">gtk-media-forward</item>
     <item stock=\"gtk-media-next\">gtk-media-next</item>
     <item stock=\"gtk-media-pause\">gtk-media-pause</item>
     <item stock=\"gtk-media-play\">gtk-media-play</item>
     <item stock=\"gtk-media-previous\">gtk-media-previous</item>
     <item stock=\"gtk-media-record\">gtk-media-record</item>
     <item stock=\"gtk-media-rewind\">gtk-media-rewind</item>
     <item stock=\"gtk-media-stop\">gtk-media-stop</item>
     <item stock=\"gtk-network\">gtk-network</item>
     <item stock=\"gtk-new\">gtk-new</item>
     <item stock=\"gtk-no\">gtk-no</item>
     <item stock=\"gtk-ok\">gtk-ok</item>
     <item stock=\"gtk-open\">gtk-open</item>
     <item stock=\"gtk-paste\">gtk-paste</item>
     <item stock=\"gtk-preferences\">gtk-preferences</item>
     <item stock=\"gtk-print\">gtk-print</item>
     <item stock=\"gtk-print-preview\">gtk-print-preview</item>
     <item stock=\"gtk-properties\">gtk-properties</item>
     <item stock=\"gtk-quit\">gtk-quit</item>
     <item stock=\"gtk-redo\">gtk-redo</item>
     <item stock=\"gtk-refresh\">gtk-refresh</item>
     <item stock=\"gtk-remove\">gtk-remove</item>
     <item stock=\"gtk-revert-to-saved\">gtk-revert-to-saved</item>
     <item stock=\"gtk-save\">gtk-save</item>
     <item stock=\"gtk-save-as\">gtk-save-as</item>
     <item stock=\"gtk-select-color\">gtk-select-color</item>
     <item stock=\"gtk-select-font\">gtk-select-font</item>
     <item stock=\"gtk-sort-ascending\">gtk-sort-ascending</item>
     <item stock=\"gtk-sort-descending\">gtk-sort-descending</item>
     <item stock=\"gtk-spell-check\">gtk-spell-check</item>
     <item stock=\"gtk-stop\">gtk-stop</item>
     <item stock=\"gtk-strikethrough\">gtk-strikethrough</item>
     <item stock=\"gtk-undelete\">gtk-undelete</item>
     <item stock=\"gtk-underline\">gtk-underline</item>
     <item stock=\"gtk-undo\">gtk-undo</item>
     <item stock=\"gtk-yes\">gtk-yes</item>
     <item stock=\"gtk-zoom-100\">gtk-zoom-100</item>
     <item stock=\"gtk-zoom-fit\">gtk-zoom-fit</item>
     <item stock=\"gtk-zoom-in\">gtk-zoom-in</item>
     <item stock=\"gtk-zoom-out\">gtk-zoom-out</item>
    </tree>
   </vbox>
  </notebook>
  <hbox>
   <button ok></button>
   <button cancel></button>
  </hbox>
 </vbox>
</window>
"
	gtkdialog3 --program PREVIEW_DIALOG
	;;
	-about)
	export ABOUT_DIALOG="
<window title=\"$LOC_ABOUT_TITLE\" icon-name=\"gtk-about\" window-position=\"1\">
 <vbox>
  <text use-markup=\"true\">
   <label>\"<b>$LOC_THEMECHOOSER</b>\"</label>
  </text>
  <frame About>
   <text>
    <label>$LOC_ABOUT_TEXT</label>
   </text>
  </frame>
  <hbox>
   <button>
    <label>$LOC_CLOSE</label>
    <input file stock=\"gtk-close\"></input>
   </button>
  </hbox>
 </vbox>
</window>
"
	gtkdialog3 --program ABOUT_DIALOG
	;;
	-gtkinstall)
	export THEME_CHOOSER="
<window title=\"$LOC_SELECT_THEME\" icon-name=\"gtk-open\">
 <vbox>
  <chooser>
   <height>300</height><width>500</width>
   <variable>THEME_ARCHIVE</variable>
  </chooser>
  <hbox>
   <button cancel>
    <action>EXIT:abort</action>
   </button>
   <button ok></button>
  </hbox>
 </vbox>
</window>
"
gtkdialog3 --program THEME_CHOOSER > $HOME/.config/themearchive.txt
. $HOME/.config/themearchive.txt
rm -f $HOME/.config/themearchive.txt
[ "$EXIT" = "abort" ] && exit

# determine the archive format
if [ ! "`echo $THEME_ARCHIVE | grep .tar.gz`" = "" ];then
 ARCHIVE_FORMAT=tgz
fi
if [ ! "`echo $THEME_ARCHIVE | grep .tar.bz2`" = "" ];then
 ARCHIVE_FORMAT=tbz
fi
if [ ! "`echo $THEME_ARCHIVE | grep .zip`" = "" ];then
 ARCHIVE_FORMAT=zip
fi

# if root, install globally
case `whoami` in
 root)
 if [ "$2" = "-icontheme" ];then
  THEME_PATH="/usr/share/icons"
 else
  THEME_PATH="/usr/share/themes"
 fi
 ;;
 *)
 if [ "$2" = "-icontheme" ];then
  THEME_PATH="$HOME/.icons"
 else
  THEME_PATH="$HOME/.themes"
 fi
 ;;
esac

export SUCCESS_DIALOG="
<window title=\"$LOC_SUCCESS\" window-position=\"2\">
 <vbox>
  <text>
   <label>$LOC_SUCCESS_TEXT</label>
  </text>
  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
</window>
"

export FAILURE_DIALOG="
<window title=\"$LOC_FAILED\" window-position=\"2\">
 <vbox>
  <text>
   <label>$LOC_FAILED_TEXT</label>
  </text>
  <hbox>
   <button ok></button>
  </hbox>
 </vbox>
</window>
"

case $ARCHIVE_FORMAT in
 tgz)
 tar xzf "$THEME_ARCHIVE" -C "$THEME_PATH"
 if [ $? -eq 0 ];then
  gtkdialog3 --program SUCCESS_DIALOG
 else
  gtkdialog3 --program FAILURE_DIALOG
 fi
 ;;
 tbz)
 tar xjf "$THEME_ARCHIVE" -C "$THEME_PATH"
 if [ $? -eq 0 ];then
  gtkdialog3 --program SUCCESS_DIALOG
 else
  gtkdialog3 --program FAILURE_DIALOG
 fi
 ;;
 zip)
 CURDIR="`pwd`"
 cd "$THEME_PATH"
 unzip "$THEME_ARCHIVE"
 if [ $? -eq 0 ];then
  gtkdialog3 --program SUCCESS_DIALOG
 else
  gtkdialog3 --program FAILURE_DIALOG
 fi
 cd "$CURDIR"
 ;;
 *)
 export ERROR_DIALOG="
 <window title=\"$LOC_BADFORMAT\" icon-name=\"gtk-dialog-error\" window-position=\"2\">
  <vbox>
   <text>
    <label>$LOC_BADFORMAT_TEXT</label>
   </text>
   <hbox>
    <button ok></button>
   </hbox>
  </vbox>
 </window>
 "
 gtkdialog3 --program ERROR_DIALOG
 exit
 ;;
esac

	;;

esac






